1594B - Special Numbers - CodeForces Solution


bitmask math *1100

Please click on ads to support us..

Python Code:

class Solution:
    def specialNumber(self, n, k):
        k = bin(k)[2:]
        
        k = k[::-1]
      
        count = 0
        ans = 0
        for i in range(len(k)):
            if k[i] == "0":
                count+=1
                continue
            ans+= n**count
            count+=1
        return ans%(10**9 + 7)
    
t = int(input())
obj = Solution()
 
for _ in range(t):
    n, k = list(map(int, input().split()))
    print(obj.specialNumber(n, k))

C++ Code:

    // Coder: Luis Miguel Casañ González
    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    ll MOD = 1e9+7;

    ll pot(int n, int p)
    {
        if(p == 0) return 1;
        
        ll ans = pot(n, p/2);
        ans = (ans*ans)%MOD;
        
        if(p&1) ans = (n*ans)%MOD;
    return ans;
    }


    ll sol()
    {
        int n, k;
        cin >> n >> k;

        ll ans = 0;
        for(int i = 0; (1<<i) <= k; i++)
        {
            if(k&(1<<i))
                ans = (ans + pot(n, i))%MOD; 
        }
    return ans;    
    }
    int main()
    {   
        #ifdef DEBUG 
            freopen("a.in", "r", stdin);
        #else
            ios_base::sync_with_stdio(0);
            cin.tie(0);
        #endif

        
        int t;
        cin >> t;

        while (t--)
        {
            cout << sol() << "\n";
        }
        
        
 
    }


Comments

Submit
0 Comments
More Questions

841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum